package com.xinyue.authorization.dao.impl;

import com.xinyue.authorization.bean.RoleOrg;
import com.xinyue.authorization.dao.RoleOrgDao;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.xinyue.authorization.bean.Role;
import com.xinyue.authorization.bean.Organization;

@Repository("RoleOrgDaoImpl")
public class RoleOrgDaoImpl implements RoleOrgDao {
	@Autowired
    private SessionFactory factory;
    
	//默认方法
	public void save(RoleOrg entity) {
		factory.getCurrentSession().save(entity);
	}
	
	public Integer deleteById(int id) {
		Query q = factory.getCurrentSession().createQuery("delete RoleOrg where id=?");
		q.setParameter(0, id);
		return q.executeUpdate();
	}
	
	@SuppressWarnings("unchecked")
	public List<Role> queryRoleByOrgId(int orgId) {
		Query q = factory.getCurrentSession().createQuery("select o from RoleOrg a,Role o where o.roleId=a.roleId and a.orgId=?");
		q.setParameter(0, orgId);
		return q.list();
	};
	
	@SuppressWarnings("unchecked")
	public List<Organization> queryOrganizationByRoleId(int roleId) {
		Query q = factory.getCurrentSession().createQuery("select o from RoleOrg a,Organization o where o.orgId=a.orgId and a.roleId=?");
		q.setParameter(0, roleId);
		return q.list();
	};
	
	public Integer deleteByRoleId(int roleId) {
		Query q = factory.getCurrentSession().createQuery("delete RoleOrg where roleId=:roleId");
		q.setParameter(0, roleId);
		return q.executeUpdate();
	};
	
	public Integer deleteByOrgId(int orgId) {
		Query q = factory.getCurrentSession().createQuery("delete RoleOrg where orgId=:orgId");
		q.setParameter(0, orgId);
		return q.executeUpdate();
	};
}